<?php

namespace App\Http\Middleware;

use App\Constants\UserStatusConstant;
use App\Helpers\Logger\SLogger;
use App\Helpers\RestUtils;
use App\Models\Chain\InviteCash\DoIdentityHandler;
use App\Models\Factory\IdentityFactory;
use App\Models\Factory\UserFactory;
use App\Models\Factory\UserStatusFactory;
use Closure;
use App\Helpers\RestResponseFactory;


/**
 * 验证身份状态中间件
 *
 * Class TokenMiddleware
 * @package App\Http\Middleware
 */
class IdentityStatusMiddleware
{
    public function handle($request, Closure $next)
    {
        $result =  $next($request);

        $user = $request -> get('user');

        //验证是否成功完成
        $isComplete = $request -> get('isComplete');

        if($isComplete) {

            //获取用户身份信息状态
            $step1 = IdentityFactory::getIdentity($user->id);
            //修改用户状态表里个人基本信息状态
            $resultIdentity = UserStatusFactory::updateUserStatus($user->id, $step1, UserStatusConstant::BACIK);


//                    通过判断完成状态开始调用责任链
            if ($step1) {

                //调用邀请好友得现金的责任链
                $user['user_id'] = $user->id;
                $inviteUserObj = new  DoIdentityHandler($user);
                $inviteuserResult = $inviteUserObj->handleRequest();
                SLogger::getStream()->error($inviteuserResult['error']);

            }

        }

        return $result;

    }

}
